<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */

class UserController extends Controller{
    
    public function actionLogin(){
        
        $user = new user();
        
        //si vamos a logearnos entramos aqui
        
        if (isset($_POST['entrar'])){
            $user->setAttributes($_POST);
            
            if ($user->login == ""){
                TForm::addMessageError("validate_login", "El campo usuario esta vacio por favor verifique"); 
            }
            else if ($user->password == ""){
                TForm::addMessageError("validate_login", "El campo clave esta vacio por favor verifique"); 
            }
            else if ($user->Count("login = :login AND password = :password",array(":login"=>$user->login,":password"=>md5($user->password))) == 0){
                TForm::addMessageError("validate_login", "Usuario o Clave Son Incorrectos por favor verifique");
            }
            else{
                
                $userData = user::model()->find("login = :login AND password = :password",array(":login"=>$user->login,":password"=>md5($user->password)));
                print "aqui";
                $this->redirec('../home/menu');
                
            }
            
        }
        
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = VeCms::createUrl("./login");
        
        $this->user_label = new TLabel("Usuario:");
        $this->user_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->user_entry = new TEdit();
        $this->user_entry->Name = "login";
        $this->user_entry->Id = $this->user_entry->Name;
        $this->user_entry->Type = "text";
        $this->user_entry->Width = 250;
        $this->user_entry->Height = 25;
        $this->user_entry->Text = $user->login;
        $this->user_entry->OptionsAditional = array('style'=>array('border:1px solid black'));
        
        $this->passwd_label = new TLabel("Clave:");
        $this->passwd_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->passwd_entry = new TEdit();
        $this->passwd_entry->Width = 250;
        $this->passwd_entry->Height = 25;
        $this->passwd_entry->Name = "password";
        $this->passwd_entry->Id = $this->passwd_entry->Name;
        $this->passwd_entry->Type = "password";
        $this->passwd_entry->Text = $user->password;
        $this->passwd_entry->OptionsAditional = array('style'=>array('border:1px solid black'));
        
        $this->button_entrar = new TButton("Entrar");
        $this->button_entrar->Name = "entrar";
        $this->button_entrar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        
    }
    
    
    public function actionNew(){
        
        $user = new user();
        
        if(isset($_POST['guardar'])){
            
            $_POST['date_register'] = time();
            $_POST['date_last_connect'] = time();
            $user->setAttributes($_POST);
            $user->password = md5($user->password);
            if ($user->validate()){
                if ($user->save()){
                    TForm::addMessage("registrado", "El registro se realizo correctamente");
                    $this->redirec(VeCms::createUrl('./grid'),10);
                }
            }
        }
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = VeCms::createUrl('./new');

        $this->first_name_label = new TLabel("Nombre:");
        $this->first_name_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->first_name_entry = new TEdit();
        $this->first_name_entry->Name = "first_name";
        $this->first_name_entry->Id = $this->first_name_entry->Name;
        $this->first_name_entry->Width = 250;
        $this->first_name_entry->Height = 25;
        $this->first_name_entry->Type = 'text';
        $this->first_name_entry->Text = $user->first_name;
        $this->first_name_entry->OptionsAditional['style'] = array('border:1px solid black');

        $this->last_name_label = new TLabel("Apellido:");
        $this->last_name_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->last_name_entry = new TEdit();
        $this->last_name_entry->Name = "last_name";
        $this->last_name_entry->Id = $this->last_name_entry->Name;
        $this->last_name_entry->Width = 250;
        $this->last_name_entry->Height = 25;
        $this->last_name_entry->Type = 'text';
        $this->last_name_entry->Text = $user->last_name;
        $this->last_name_entry->OptionsAditional['style'] = array('border:1px solid black');

        $this->email_label = new TLabel("Correo:");
        $this->email_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->email_entry = new TEdit();
        $this->email_entry->Name = "e_mail";
        $this->email_entry->Id = $this->email_entry->Name;
        $this->email_entry->Width = 250;
        $this->email_entry->Height = 25;
        $this->email_entry->Type = 'text';
        $this->email_entry->Text = $user->e_mail;
        $this->email_entry->OptionsAditional['style'] = array('border:1px solid black');

        $this->phone_label = new TLabel("Telefono:");
        $this->phone_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->phone_entry = new TEdit();
        $this->phone_entry->Name = "phone";
        $this->phone_entry->Id = $this->phone_entry->Name;
        $this->phone_entry->Width = 250;
        $this->phone_entry->Height = 25;
        $this->phone_entry->Type = 'text';
        $this->phone_entry->Text = $user->phone;
        $this->phone_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->phone_mobile_label = new TLabel("Celular:");
        $this->phone_mobile_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->phone_mobile_entry = new TEdit();
        $this->phone_mobile_entry->Name = "phone_mobile";
        $this->phone_mobile_entry->Id = $this->phone_mobile_entry->Name;
        $this->phone_mobile_entry->Width = 250;
        $this->phone_mobile_entry->Height = 25;
        $this->phone_mobile_entry->Type = 'text';
        $this->phone_mobile_entry->Text = $user->phone_mobile;
        $this->phone_mobile_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->address_label = new TLabel("Direccion:");
        $this->address_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->address_textview = new TTextView();
        $this->address_textview->Name = "address";
        $this->address_textview->Id = $this->address_textview->Name;
        $this->address_textview->Cols = "10";
        $this->address_textview->Rows = "10";
        $this->address_textview->Width = 250;
        $this->address_textview->Height = 100;
        $this->address_textview->Text = $user->address;
        $this->address_textview->OptionsAditional = array('style'=>array('border:1px solid black'));
        
        
        $this->user_label = new TLabel("Usuario:");
        $this->user_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->user_entry = new TEdit();
        $this->user_entry->Name = "login";
        $this->user_entry->Id = $this->user_entry->Name;
        $this->user_entry->Type = "text";
        $this->user_entry->Width = 250;
        $this->user_entry->Height = 25;
        $this->user_entry->Text = $user->login;
        $this->user_entry->OptionsAditional = array('style'=>array('border:1px solid black'));
        
        //clave
        
        $this->passwd_label = new TLabel("Clave:");
        $this->passwd_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->passwd_entry = new TEdit();
        $this->passwd_entry->Width = 250;
        $this->passwd_entry->Height = 25;
        $this->passwd_entry->Name = "password";
        $this->passwd_entry->Id = $this->passwd_entry->Name;
        $this->passwd_entry->Type = "password";
        $this->passwd_entry->Text = $user->password;
        $this->passwd_entry->OptionsAditional = array('style'=>array('border:1px solid black'));
        
        //rol
        
        $this->rol_label = new TLabel("Rol:");
        $this->rol_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->rol_entry = new TComboBox();
        $this->rol_entry->Name = "id_rol";
        $this->rol_entry->Id = $this->rol_entry->Name;
        $this->rol_entry->Width = 250;
        $this->rol_entry->Height = 25;
        $this->rol_entry->Type = 'text';
        $this->rol_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        //cargando los datos de los roles
        
        $roles = rol::model()->findAll();
        foreach ($roles as $key=>$value){
            $this->rol_entry->Options[]=array($value->id_rol,$value->description);
        }
        
        //buttons
        
        $this->button_reset = new TButton("Cancelar");
        $this->button_reset->Type = "reset";
        $this->button_reset->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_entrar = new TButton("guardar");
        $this->button_entrar->Name = "guardar";
        $this->button_entrar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));

    }
    
    public function actionGrid(){
        print "hola";
    }
    
}
?>
